package com.code.bolg.controller;

import com.code.bolg.annotation.ReturnParam;
import com.code.bolg.bean.*;
import com.code.bolg.constant.RedisConstant;
import com.code.bolg.converter.StringToObject;
import com.code.bolg.mappper.ItemMapper;
import com.code.bolg.output.ArticleOutPut;
import com.code.bolg.output.IndexOutPut;
import com.code.bolg.service.ArticleService;
import com.code.bolg.service.CategoryService;
import com.code.bolg.service.SpearkService;
import com.code.bolg.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * index
 */
@Slf4j
@RestController
public class IndexController {
    @Autowired
    private SpearkService spearkService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/index")
    public Object index() {
        //        初始化
        IndexOutPut indexOutPut = new IndexOutPut();
        List<ArticleOutPut> ouputList = new ArrayList<>();
        List<Article> all = articleService.findAll(null, 0, 10).getContent();
//        数据库查询
        List<User> userList = userService.findAll();
        List<Category> categoryList = categoryService.findAll();

        //公告
        Speak speak = new Speak();
        speak.setTitle(spearkService.find(RedisConstant.SPEAK));

        //ariaceList
        for (Article model : all) {
            ArticleOutPut outPut = new ArticleOutPut();
            BeanUtils.copyProperties(model, outPut);

            Optional<User> userOptional = userList.stream().filter(item -> item.getUserId() == model.getUserId()).findFirst();
            Optional<Category> categoryOptional = categoryList.stream().filter(item -> item.getCategoryId() == model.getCategoryId()).findFirst();

            //设值用户信息
            if (userOptional.isPresent()) {
                User user = userOptional.get();
                outPut.setUserId(user.getUserId());
                outPut.setAnthor(user.getUsername());
            }

            //设值分类信息
            if (categoryOptional.isPresent()) {
                Category category = categoryOptional.get();
                outPut.setCategoryId(category.getCategoryId());
                outPut.setCategoryName(category.getCategoryName());
            }
            ouputList.add(outPut);
        }


        indexOutPut.setAriaceList(ouputList);
        indexOutPut.setSpeaker(speak);
        return indexOutPut;
    }

    @RequestMapping("/navItems")
    public  Object
    navItems(){
        List<Item> list = jdbcTemplate.query("select * from bolg_item bi ORDER BY bi.id", new ItemMapper());
        return list;
    }

    @RequestMapping("/test")
    @ReturnParam(isEnum = true)
    public  Object
    test(){
        return HttpStatus.OK;
    }
    @RequestMapping("/test1")
    @ReturnParam(isPackage = false)
    public  Object
    test1(){
        return HttpStatus.OK;
    }
    @RequestMapping("/test2")
    @ReturnParam(isPackage = false)
    public  Object
    test2(){
        return HttpStatus.OK;
    }

}
